package com.geostart.constructionland.system.controller.indicator;

import java.util.Date;
import java.util.List;

import com.geostart.constructionland.system.entity.RepEntity;
import com.geostart.constructionland.system.entity.indicator.CityPlanIndicatorEntity;
import com.geostart.constructionland.system.service.indicator.CityPlanIndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 导入controller
 *
 * @author chenjianjun
 * @since 2020-09-08 14:22
 */
@RestController
@Slf4j
public class CityPlanIndicatorController {
    private CityPlanIndicatorService cityPlanIndicatorService;

    @Autowired
    public CityPlanIndicatorController(CityPlanIndicatorService cityPlanIndicatorService) {
        this.cityPlanIndicatorService = cityPlanIndicatorService;
    }

    @ApiOperation(value = "市计划指标导入")
    @PostMapping("zb/city-plan-indicator/import-excel")
    public RepEntity cityPlanIndicatorImport(@RequestParam("file") MultipartFile multipartFile,
                                             @ApiParam(value = "用户id", required = true) @RequestParam("createUserId") String createUserId,
                                             @ApiParam(value = "用用户名字", required = true) @RequestParam("createUserName") String createUserName,
                                             @ApiParam(value = "是否覆盖导入",
                                    required = false) @RequestParam("repeatImportFlag") Integer repeatImportFlag) {
        RepEntity repEntity = new RepEntity();
        try {
            cityPlanIndicatorService.cityPlanIndicatorImport(multipartFile, createUserId, createUserName,
                            repeatImportFlag);
            repEntity.setCode("200");
            return repEntity;
        } catch (Exception e) {
            log.info("导入失败");
            log.info(e.getMessage());
            e.printStackTrace();
            repEntity.setCode("500");
            repEntity.setContent(e.getMessage());
            return repEntity;
        }
    }

    @ApiOperation(value = "校验市级指标今日是否重复导入")
    @PostMapping("zb/city-plan-indicator/import-excel/is-repeat-import")
    public RepEntity isRepeatImport(@RequestParam("file") MultipartFile multipartFile) {
        return cityPlanIndicatorService.isRepeatImport(multipartFile);
    }

    @ApiOperation(value = "市级计划指标list")
    @GetMapping("zb/city-plan-indicator/list")
    public RepEntity list(
                    @ApiParam(value = "指标日期(版本)", required = true) @RequestParam("indicatorDate") Date indicatorDate) {
        RepEntity repEntity = new RepEntity();
        List<CityPlanIndicatorEntity> list = cityPlanIndicatorService
                        .list(CityPlanIndicatorEntity.builder().indicatorDate(indicatorDate).build());
        String result = JSONObject.toJSONString(list);
        repEntity.setCode("200");
        repEntity.setContent(result);
        return repEntity;
    }
}
